<template>
    <template v-if="completed">
        <router-view />
        <Ywatermark :info="userinfoText"></Ywatermark>
    </template>
    <z-dialog v-model:show="showTips"content="禁止复制" @confirm="showTips = false"></z-dialog>
</template>
<script setup>
import useAppStore from '@/store/app'
import useConfigStore from '@/store/config'
import { closeWebSocket, initWebSocket, sendWebSocket } from '@/pkg/websocket'
import ls from '@/lib/ls'
import { watch } from 'vue';
import Ywatermark from '@/components/watermark/watermark.vue'

const configStore = useConfigStore()
const appStore = useAppStore()
const completed = computed(() => appStore.completed)
const userinfoText = ref('')

// ----------------------------------------------- 长链接 ----------------------------------------------------------
console.log(configStore.isLoad);
// 监听是否登录
watch(configStore.isLoad,(newValue,oldValue)=>{
  console.log(newValue);
    if(newValue.state == 1){
      connectMsg()
      userinfoText.value = ls.get('USER_INFO') ? ls.get('USER_INFO').userName + '  ' +  ls.get('USER_INFO').loginTime : '' 
    }else{
      closeWs()
    }
})

// 连接设备  （具体路径和后端规定）
const connectMsg = () => {
  if (!configStore.isConnected) {
    let toIp = import.meta.env.VITE_WS
    configStore.connect()
    initWebSocket(toIp)
  }
}
const userInfo = ls.get('USER_INFO')
if(userInfo){ // 如果已经登录 自动连接ws
  configStore.isLoad.state = 1
}
if(configStore.isLoad.state == 1 && userInfo){
  connectMsg()
}

// 设备断开
const closeWs = () => {
  if (configStore.isConnected) {
    closeWebSocket()
    configStore.disconnect()
    console.log('退出');
  }
}
// 禁用右键菜单
const disableRightClick = (event) => {
  event.preventDefault();
}

// 禁用复制操作
const disableCopy = (event) => {
  event.preventDefault();
}

// 禁用快捷键（Ctrl + C / Cmd + C）
const disableCopyShortcut = (event) => {
  if ((event.ctrlKey || event.metaKey) && (event.key === 'c' || event.key === 'C')) {
    event.preventDefault();
  }
}

const showTips = ref(false)
let tiemout = null
const startPress = ()=>{
  tiemout = setTimeout(()=>{
    if(ls.get('ORG_CODE') == '0015') {
        showTips.value = true
    }
  },1000)
  
}
const endPress = ()=>{

  clearTimeout(tiemout)
 
}

onMounted(() => {
  document.addEventListener('contextmenu', disableRightClick);
  document.addEventListener('copy', disableCopy);
  document.addEventListener('keydown', disableCopyShortcut);
  document.body.style.userSelect = 'none';  // 禁止文本选择

  document.addEventListener('touchstart', startPress);
  document.addEventListener('touchend', endPress);
  

});

onBeforeUnmount(() => {
  document.removeEventListener('contextmenu', disableRightClick);
  document.removeEventListener('copy', disableCopy);
  document.removeEventListener('keydown', disableCopyShortcut);
  document.body.style.userSelect = '';  // 恢复文本选择
});

</script>
<style scoped lang="scss">
*{

  -webkit-touch-callout:none; /*系统默认菜单被禁用*/

  -webkit-user-select:none; /*webkit浏览器*/

  -khtml-user-select:none; /*早期浏览器*/

  -moz-user-select:none;/*火狐*/

  -ms-user-select:none; /*IE10*/

  user-select:none;

}

</style>